//
// Created by kali on 1/25/22.
//

#ifndef SNAKYRUN_CMSERVICEBASE_H
#define SNAKYRUN_CMSERVICEBASE_H

#include <iostream>
#include <list>
#include <map>

#include "CMLock.h"

typedef int *cm_service_base_callback(int type, void *args);

class CMServiceBaseFunction
{
public:
    virtual int onRecv(int type, void *args) = 0;
};

typedef std::map<int, std::list<cm_service_base_callback *> > service_base_callback_map;
typedef std::map<int, std::list<CMServiceBaseFunction*> > service_base_function_map;

class CMServiceBase {
public:
    CMServiceBase(const char *name);
    virtual ~CMServiceBase();

    virtual int init() { return 0; };
    virtual int deinit() { return 0; };
    virtual int start() { return 0; };
    virtual int stop() { return 0; };
    virtual int ctl(int type, void *args) { return 0; };
    int registerRecv(int type, cm_service_base_callback *cb);
    int registerRecv(int type, CMServiceBaseFunction *func);

    std::string name()
    {
        return m_name;
    }

protected:
    void clear();

    service_base_callback_map m_cb_list;
    service_base_function_map m_func_list;

private:
    std::string m_name;
    CMLock  m_lock;
};


#endif //SNAKYRUN_CMSERVICEBASE_H
